﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Areas.Eleve.Models;
using SchoolProject2012.Functions;

namespace SchoolProject2012.Areas.Eleve.Controllers
{
    public class SondageController : Controller
    {
        private EleveSchoolProjectBddEntities db = new EleveSchoolProjectBddEntities();
        //
        // GET: /Eleve/Sondage/

        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                var sondage = db.SONDAGE.Include("CLASSE").Where(x => x.ELEVE.LOGINPERS == login).ToList();
                return View(sondage);
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Details(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                List<QUESTIONSONDAGE> questions = db.QUESTIONSONDAGE.Where(x => x.IDSONDAGE == id).ToList();
                ViewBag.id = id;
                return View(questions);
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Delete(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                SONDAGE sondage = db.SONDAGE.Single(x => x.IDSONDAGE == id);
                return View(sondage);
            }
            catch
            {
                return View("error");
            }
        }


        [HttpPost]
        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Delete(string langue, SONDAGE sondageToDelete)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                db.SONDAGE.DeleteObject(sondageToDelete);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Edit(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                SONDAGE sondage = db.SONDAGE.Single(x => x.IDSONDAGE == id);
                return View(sondage);
            }
            catch
            {
                return View("error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Edit(string langue, SONDAGE sondageToEdit)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                db.Refresh(System.Data.Objects.RefreshMode.StoreWins, sondageToEdit);
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult repondre(string langue, int id, int idQuestion)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                var reponse = db.QUESTIONSONDAGE.Single(x => x.IDQUESTSON == idQuestion && x.IDSONDAGE == id);
                return View(reponse);
            }
            catch
            {
                return View("error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult repondre(string langue, REPONSESONDAGE rep)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Créer(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Admin, BDE")]
        public ActionResult Créer(string langue, SONDAGE sondage)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                int classe = db.ELEVE.Single(x => x.LOGINPERS == login).ELEVECLASS.Single(x => x.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS;
                db.SONDAGE.AddObject(sondage);
                db.SaveChanges();
                return View("index");
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, BDE")]
        public ActionResult AjoutQuestion(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Admin, BDE")]
        public ActionResult AjoutQuestion(string langue, int id, QUESTIONSONDAGE quest)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                db.QUESTIONSONDAGE.AddObject(quest);
                db.SaveChanges();
                return View("details", db.QUESTIONSONDAGE.Where(x => x.IDSONDAGE == id).ToList());
            }
            catch
            {
                return View("error");
            }
        }

        [Authorize(Roles = "Admin, BDE")]
        public ActionResult AjoutReponse(string langue, int id, int question)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                ViewBag.question = question;
                ViewBag.sondage = id;
                return View();
            }
            catch
            {
                return View("error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Admin, BDE")]
        public ActionResult AjoutReponse(string langue, int id, int question, REPONSESONDAGE resp)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                QUESTIONSONDAGE quest = db.QUESTIONSONDAGE.Single(x => x.IDQUESTSON == id && x.IDSONDAGE == question);
                resp.IDQUESTSON = quest.IDQUESTSON;
                quest.REPONSESONDAGE.Add(resp);
                db.SaveChanges();
                return View("details", db.QUESTIONSONDAGE.Where(x => x.IDSONDAGE == id).ToList());
            }
            catch
            {
                return View("error");
            }
        }
    }
}
